﻿/// ZaxControls
/// Programmed by Leland Ede
/// Website: http://www.zaxisstudios.com
/// E-Mail: codeplex@zaxis-studios.com
/// Create: 3/29/2014
/// Modified: 4/09/2014

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ZaxLib.Web.Writers.Nodes;

namespace ZaxLib.Web.UI.WebControls
{
	[DefaultProperty("Text")]
	[ToolboxData("<{0}:TextBox runat=server></{0}:TextBox>")]
	public class TextBox : ZaxControl
	{
		#region Parameters

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public String Name
		{
			get
			{
				String s = ViewState["Name"] as String;
				return (s == null ? String.Empty : s);
			}
			set
			{
				ViewState["Name"] = value;
			}
		}

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public String Size
		{
			get
			{
				String s = ViewState["Size"] as String;
				return (s == null ? String.Empty : s);
			}
			set
			{
				ViewState["Size"] = value;
			}
		}

		[Bindable(true)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public String AutoItems
		{
			get
			{
				String s = ViewState["AutoItems"] as String;
				return (s == null ? String.Empty : s);
			}
			set
			{
				ViewState["AutoItems"] = value;
			}
		}

		[Bindable(false)]
		[Category("Appearance")]
		[DefaultValue("")]
		[Localizable(true)]
		public String Type
		{
			get
			{
				String s = ViewState["Type"] as String;
				return (s == null ? "Text" : s);
			}
			set
			{
				ViewState["Type"] = value;
			}
		}

		private ArrayList _autoComplete = null;
		public ArrayList AutoComplete
		{
			get
			{
				if (_autoComplete == null)
				{
					_autoComplete = new ArrayList();
					// See if we need to load the list
					if (AutoItems != String.Empty)
					{
						String[] items = AutoItems.Split(',');
						foreach (String item in items)
						{
							_autoComplete.Add(String.Format("\"{0}\"", item.Trim()));
						}// End foreach item in items list
					}// Endif AutoItems has data stored in it
				}// Endif _autoComplete is null

				return _autoComplete;
			}// end get
		}// End parameter AutoComplete

		#endregion

		#region Control Writers

		/// <summary>
		/// Write TextBox control to the client.
		/// </summary>
		/// <param name="writer">HTML Writer object.</param>
		protected override void ControlWrite(HtmlTextWriter writer)
		{
			base.ControlWrite(writer);
			// Add input attributes
			writer.AddAttribute(HtmlTextWriterAttribute.Id, ID);
			// Set UniquID to name attribute of tag
			writer.AddAttribute(HtmlTextWriterAttribute.Name, UniqueID);
			// Add value to input tag
			writer.AddAttribute(HtmlTextWriterAttribute.Value, Text);
			// Check if the Type is password
			if (Type.ToUpper() == "PASSWORD")
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Type, "password");
			}
			// Check if the CssClass is not empty
			if (CssClass != String.Empty)
			{
				// Add the CssClass to tag for styling
				writer.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
			}
			// Check if the Size property is not empty
			if (Size != String.Empty)
			{
				// Add size to the input tag
				writer.AddAttribute(HtmlTextWriterAttribute.Size, Size);
			}
			// Start rendering the input tag
			writer.RenderBeginTag(HtmlTextWriterTag.Input);
			// End tag since input doesn't typically use a closing tag
			writer.RenderEndTag();

			// Check to see if there are any AutoComplete 
			if (AutoComplete.Count > 0)
			{
				//CodeNode parent = CodeWriter.AddCode(String.Format("#{0}", ID), NodeType.NT_SELECTOR);
				writer.Write("\n");

				String listVar = String.Format("{0}List", ID);

				// Crate a top reference node
				INode node = CodeWriter.CreateTopNode();
				// Create a function selector
				node = CodeWriter.AddChild("function").SelectorFunction();
				// Add variable to function definition
				CodeWriter.AddChild(listVar).Array();
				CodeWriter.AddChild(listVar).VarDef();
				foreach (String item in AutoComplete)
				{
					CodeWriter.AddParam(item);
				}

				// Create a selector for input
				CodeWriter.AddChild(ID, node).SelectorId();
				// Turn input into an auto complete input box
				CodeWriter.AddChild("autocomplete").Joined();
				// Create auto complete object
				node = CodeWriter.AddParam("").ObjectDef();
				// Add source array name to object
				CodeWriter.AddParam("source", listVar, node).VarDef();
			}// Endif AutoComplete as more than one element defined

			// Check if there is a OnChange event tied to this control
			if (IsChange)
			{
				INode node = CodeWriter.CreateTopNode();
				CodeWriter.AddChild(ID).SelectorId();
				CodeWriter.AddChild("change").Joined();
				node = CodeWriter.AddParam("function").Function();
				INode pNode = CodeWriter.AddChild(node).Variable();
				CodeWriter.AddChild(String.Format("{0}Value", ID), String.Format("\"{0}\"", Text), pNode).VarDef();
				CodeWriter.AddChild(String.Format("if({0}Value != $(\"#TextID\").value){{", ID), node).RawCode();
				CodeWriter.AddChild(String.Format("\t__doPostBack(\"{0}\",\"\");", ID), node).RawCode();
				CodeWriter.AddChild("}", node).RawCode();
			}
		}


		#endregion

		#region Render Methods

		protected override void RenderContents(HtmlTextWriter writer)
		{
			base.RenderContents(writer);
		}

		#endregion
	}
}
